package cn.swenty.backstage.base.web.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.swenty.backstage.base.web.system.entity.SysDepFormMap;
import cn.swenty.backstage.base.web.system.service.DepartService;
import cn.swenty.backstage.framework.common.util.StringUtil;
import cn.swenty.backstage.framework.controller.BaseController;
import cn.swenty.backstage.framework.database.plugin.PageView;
import cn.swenty.backstage.framework.util.Common;
import cn.swenty.backstage.framework.util.IdGenerator;

/**
 * 部门管理
 * @author LiuHao
 *
 */

@Controller
@RequestMapping("/system/dept/")
public class DeptControl extends BaseController {
	@Autowired
	private DepartService departService;
	
	
	@RequestMapping("info")
	public String infoUI(Model model) throws Exception {
		String depId = getPara("dep_id");
		//修改时需要将父级和本级都加载
		if(Common.isNotEmpty(depId)){
			//部门
			SysDepFormMap sdfm = departService.get(SysDepFormMap.class, depId);
			model.addAttribute("dept", sdfm);
			
			
		}
		
		return Common.BACKGROUND_PATH + "/system/dept/info";
	}
	
	@RequestMapping("list")
	public String listDep(Model model) throws Exception {
		getHttpRequest().setAttribute("list",departService.findDeptTree());

		return Common.BACKGROUND_PATH + "/system/dept/list";
	}
	
	/**
	 * 添加部门
	 * 
	 * @param depart
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping("addEntity")
	@ResponseBody
	public String addEntity() {
		String setting_id = getPara("setting_id");
		
			SysDepFormMap sysDepFormMap = getFormMap(SysDepFormMap.class);
			sysDepFormMap.put("dept_id", IdGenerator.getNextId());
			sysDepFormMap.put("path", sysDepFormMap.get("path")+"/"+sysDepFormMap.getStr("dept_id"));
			departService.saveDept(sysDepFormMap,setting_id);//新增后返回新增信息

		return "success";
	}
	
	
	@RequestMapping("deptTree")
	public String tree(){
		getHttpRequest().setAttribute("deptTree", departService.findDeptTree());
		getHttpRequest().setAttribute("hiddenDeptTree", departService.findHiddens());

		return Common.BACKGROUND_PATH+"/system/dept/deptTree";
	}
	
	
	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage( String pageNow,
			String pageSize,String column,String sort) throws Exception {
		SysDepFormMap sysDepFormMap = getFormMap(SysDepFormMap.class);
		String dept_state =(String) sysDepFormMap.get("dept_state");
		if(dept_state==null)
			sysDepFormMap.put("dept_state", 0);   //查在职
		else if(dept_state.equals("2")){
			sysDepFormMap.remove("dept_state");
		}
		
		if(StringUtil.isNull(sysDepFormMap.getStr("orderby"))){
			sysDepFormMap.put("orderby", " order by sequence asc");
		}
		sysDepFormMap=toFormMap(sysDepFormMap, pageNow, pageSize,sysDepFormMap.getStr("orderby"));
		sysDepFormMap.put("column", column);
		sysDepFormMap.put("sort", sort);
		pageView.setRecords(departService.find(sysDepFormMap));//不调用默认分页,调用自已的mapper中findDepPage
        return pageView;
	}
	
	@ResponseBody
	@RequestMapping("byParentid")
	public PageView byParentid(String pageNow,
			String pageSize,String column,String sort) {
		SysDepFormMap sysDepFormMap = getFormMap(SysDepFormMap.class);
		sysDepFormMap.put("parent_id", getPara("parent_id"));
		sysDepFormMap=toFormMap(sysDepFormMap, pageNow, pageSize,sysDepFormMap.getStr("orderby"));
		sysDepFormMap.put("column", column);
		sysDepFormMap.put("sort", sort);
		pageView.setRecords(departService.findByPage(sysDepFormMap));
		return pageView;
	}
	
	
	@ResponseBody
	@RequestMapping("getPath")
	public String getPath( ) throws Exception {
		
		String pidroot= getPara("dep_id");
		if(pidroot==null)pidroot="root";
		SysDepFormMap sysDepFormMap = getFormMap(SysDepFormMap.class);
		List<SysDepFormMap> deplist = departService.depNode(sysDepFormMap);
		String path=pidroot;
		while (!pidroot.equalsIgnoreCase("root")){
			for(int i=0;i<deplist.size();i++){
				if(deplist.get(i).getStr("dept_id").equals(pidroot)){
					pidroot=deplist.get(i).getStr("parent_id");
					path=pidroot+"/"+path;
					break;
				}
				
			}
		}
		return path;
	}
	
	/**
	 * 判断部门类型，如果为公司则返回true，如果都为公司则返回true
	 * @return
	 */
	@ResponseBody
	@RequestMapping("judgeDeptType")
	public boolean judgeDeptType(){
		//部门ids
		String dids = getPara("dids");
		String[]ids = dids.split(",");
		for(int i=0;i<ids.length;i++){
			SysDepFormMap sfm =	departService.get(SysDepFormMap.class, ids[i]);
			if(sfm.getInt("dept_type")!=1){
				return false;
			}
		}
		return true;
	}
	
	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String editEntity(String txtGroupsSelect) throws Exception {
		SysDepFormMap sysDepFormMap = getFormMap(SysDepFormMap.class);
		String setting_id = getPara("setting_id");
		departService.updateDept(sysDepFormMap,setting_id);//新增后返回新增信息
		return "success";
	}
	
	
	@ResponseBody 
	@RequestMapping("delete")
	public String delete() throws Exception{
		String id = this.getPara("ids");
		String [] ids =id.split(",");
		for (String string : ids) {
			SysDepFormMap deptFormMap=getFormMap(SysDepFormMap.class);
			deptFormMap.put("dept_id", string);
			deptFormMap.put("dept_state", 1);
			departService.updateStatus(deptFormMap);
		}
			
		
		return "success";
	}
	
	
}
